﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.EnumerationObjects
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class PdfScanOutput:ResultBase
    {
        /// <summary>
        /// 
        /// </summary>
        public struct PdfScanResult
        {
            public bool IsElectronic;
            public bool IsScanned;
            public int[] PageNumbers;
        }

        #region props
        private List<string> _CorruptedFiles;
        /// <summary></summary>
        public List<string> CorruptedFiles
        {
            get
            {
                return this._CorruptedFiles;
            }
            set
            {
                this._CorruptedFiles = value;
            }
        }

        private string _ResultFilePath;
        /// <summary>
        /// <Files><File DocID="1" Pages="1,2,3" IsElectronic="False" IsScanned="False" /></Files>
        /// </summary>
        public string ResultFilePath
        {
            get
            {
                return this._ResultFilePath;
            }
            set
            {
                this._ResultFilePath = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public PdfScanOutput():base(Guid.Empty, Guid.Empty)
        {
            this._CorruptedFiles=new List<string>();
            this._ResultFilePath = string.Empty;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="corruptedFiles"></param>
        /// <param name="resultFilePath"></param>
        public PdfScanOutput(Guid componentID, Guid jobTicketID, 
            List<string> corruptedFiles, 
            string resultFilePath) 
            : base(componentID, jobTicketID)
        {
            this._CorruptedFiles = corruptedFiles;
            this._ResultFilePath = resultFilePath;
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xNode"></param>
        /// <returns></returns>
        public override ResultBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                PdfScanOutput output = new PdfScanOutput();

                XmlNodeList corruptedFileNodes = xNode.SelectNodes("CorruptedFiles/CorruptedFile");
                if(corruptedFileNodes !=null && corruptedFileNodes.Count>0)
                {
                    output.CorruptedFiles=new List<string>();
                    foreach(XmlNode corruptedNode in corruptedFileNodes)
                    {
                        string filePath = XmlDataUtil.GetAttributeValue(corruptedNode, "FilePath", "");
                        output.CorruptedFiles.Add(filePath);
                    }
                }

                output.ResultFilePath = XmlDataUtil.GetAttributeValue(xNode, "ResultFilePath", "");

                return output;
            }
            else
            {
                throw new Exception("Unable to instantiate output: invalid type specified in xml node");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xDoc"></param>
        /// <param name="parentNode"></param>
        /// <returns></returns>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Output");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinishTime", this.FinishTime.ToString());

            if(this.CorruptedFiles !=null && this.CorruptedFiles.Count>0)
            {
                XmlNode corruptedFileRootNode = XmlDataUtil.AddElement(ref xDoc, xNode, "CorruptedFiles");
                foreach(string corruptedFile in this.CorruptedFiles)
                {
                    XmlNode corruptedFileNode = XmlDataUtil.AddElement(ref xDoc, corruptedFileRootNode, "CorruptedFile");
                    XmlDataUtil.UpdateAttribute(ref xDoc, corruptedFileNode, "FilePath", corruptedFile);
                }
            }

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode,"ResultFilePath",this.ResultFilePath);
           
            return xNode;
        }
    }
}
